package org.tinygroup.daccase;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.tinygroup.dac.datasource.SingleDataAccessControlDataSource;

public class DACTestInitial {

	private static DACTestInitial init;

	private SingleDataAccessControlDataSource dacDataSource;

	private BasicDataSource commonDatasource;

	private ClassPathXmlApplicationContext applicationContext;
	
	private DACTestInitial() {

	}
	public static DACTestInitial getInstance() {
		if (init == null) {
			init = new DACTestInitial();
		}
		return init;
	}

	public void init() {
		this.applicationContext=new ClassPathXmlApplicationContext("dacbeans.xml");
		applicationContext.refresh();
		applicationContext.registerShutdownHook();
		this.dacDataSource=(SingleDataAccessControlDataSource) applicationContext.getBean("singleDataAccessControlDataSource");
		this.commonDatasource=(BasicDataSource) applicationContext.getBean("dataSource");
	}

	public Connection getDacConnection() throws SQLException{
		return dacDataSource.getConnection();
	}
	
	public Connection getCommonConnection() throws SQLException{
		return commonDatasource.getConnection();
	}

	public SingleDataAccessControlDataSource getDataSource() {
		return dacDataSource;
	}

	public BasicDataSource getCommonDatasource() {
		return commonDatasource;
	}
	
}
